public class Sort {
    public void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    flg = true;
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
            if(flg = false){
                break;
            }
        }
    }
    public void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public int partitionheap(int[] array,int start,int end){
        int key = array[start];
        while(start<end){
            while(start<end && array[end]>=key){
                end--;
            }
            array[start] = array[end];
            while(start<end && array[start]<=key){
                start++;
            }
            array[end] = array[start];
        }
        array[start]  = key;
        return start;
    }

    public int midNumIndex(int[] array,int left,int right){
        int mid = (left+right)/2;
        if(array[left]>array[right]){
            if(array[mid]<array[right]){
                return right;
            }else if(array[mid]>array[left]){
                return left;
            }else{
                return mid;
            }
        }else{
            if(array[mid]>array[right]){
                return right;
            }else if(array[mid]<array[left]){
                return left;
            }else{
                return mid;
            }
        }
    }
    public int partition(int[] array,int start,int end){
        int i = start;//事先存储好start的下标
        int key = array[start];//存基准值
        while(start<end){
            //第一个while循环里的start<end是如果start=end说明该序列就一个元素了那么一定是有序的
            //下面while循环里的start<end是因为如果end前的元素都大于key的话若没有这个条件end会一直--到-1那就越界了
            //为什么end对应的元素要>=key而不是>key呢？若是>key那么如果key的值与end对应元素的值相等那么程序会进入死循环
            while(start<end&&array[end]>=key){
                end--;
            }
            while(start<end&&array[start]<=key){
                start++;
            }
            swap(array,start,end);
        }
        swap(array,i,start);
        return start;
    }


    public int partition1(int[] array,int start,int end){
        int prev = start;
        int cur = start+1;
        while(cur<=end){
            if(array[cur]<array[start] && array[++prev]!=array[cur]){
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,start);
        return prev;
    }
    public void quickSort(int[] array,int left,int right){
        while(left>=right){
            return;
        }
        int index = midNumIndex(array,left,right);
        swap(array,left,index);
        int poive = partition(array,left,right);
        quickSort(array,left,poive-1);
        quickSort(array,poive+1,right);
    }
}

